package com.atguigu.flow.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.flow.entity.vo.Module_Field;
import com.atguigu.flow.entity.vo.Noclude_Field;

import java.io.*;
import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;

public
class Zxb_receivedata {



    public static
    byte[] convertFileToByteArray(String fileName) throws IOException {
        BufferedInputStream bis;
        ByteArrayOutputStream bos;
        bis = new BufferedInputStream(new FileInputStream(fileName));
        bos = new ByteArrayOutputStream();
        int data;
        while ((data = bis.read()) != -1) {
            bos.write(data);
        }
        bos.flush();
        return bos.toByteArray();
    }

    public static
    void convertByteArrayToFile(String filename, byte[] datas) throws Exception {

        if (datas != null) {
//            String filepath ="E:\\" + filename;
            File file = new File(filename);
            if (file.exists()) {
                file.delete();
            }


            BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(datas));
            FileOutputStream fileOutputStream = new FileOutputStream(filename);
            BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);

            int data;
            while ((data = bis.read()) != -1) {
                bos.write(data);
            }
            bos.flush();

        }
    }

    public static List<String> nofieldlist(List<Noclude_Field> noSelectedlist,String tablename,String[] addlist){
        List<Noclude_Field> noSelectedlisttemp=noSelectedlist.stream().filter(user ->tablename.equals(user.getTablename())).collect(Collectors.toList());
        System.out.println(noSelectedlisttemp);
        List<String> noselectlist=new ArrayList<>();
        for (int j=0;j<noSelectedlisttemp.size();j++)
            noselectlist.add(noSelectedlisttemp.get(j).getFieldname());

        Collections.addAll(noselectlist,addlist);

        return noselectlist;
    }
    public static JSONArray fieldarray(JSONObject json_old,List<Module_Field> module_fieldList,String tablename){
        Iterator iter = json_old.entrySet().iterator();

        List<Module_Field> module_fieldList_temp = module_fieldList.stream().filter(user ->tablename.equals(user.getTablename())).collect(Collectors.toList());
        JSONArray jsonArray=new JSONArray();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            JSONObject json2=new JSONObject();

            json2.put("fieldname",entry.getKey());
            json2.put("value",entry.getValue());
            String fieldname=entry.getKey().toString().toUpperCase();
            List <Module_Field> module_fieldList_temp1=module_fieldList_temp.stream().filter(user ->fieldname.equals(user.getFieldname().toUpperCase())).collect(Collectors.toList());
            if (module_fieldList_temp1.size()>0)
                json2.put("title",module_fieldList_temp1.get(0).getTitle());
            jsonArray.add(json2);

        }
        //System.out.println(module_fieldList_temp);

        return jsonArray;
    }

    public static JSONObject fieldtrans(List<Module_Field> module_fieldList,List<String> tablelist){
        JSONObject json2=new JSONObject();
        for(String tablename:tablelist) {

            List<Module_Field> module_fieldList_temp = module_fieldList.stream().filter(user -> tablename.equals(user.getTablename())).collect(Collectors.toList());
            for (int i = 0; i < module_fieldList_temp.size(); i++) {

                json2.put(module_fieldList_temp.get(i).getTablename() + "-" + module_fieldList_temp.get(i).getFieldname(), module_fieldList_temp.get(i).getTitle());


            }
        }
        //System.out.println(module_fieldList_temp);

        return json2;
    }

    public static JSONObject maintable(JSONObject json_old,String tablename){
        Iterator iter = json_old.entrySet().iterator();

        JSONObject json2=new JSONObject();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();


            json2.put(tablename+"-"+entry.getKey(),entry.getValue());



        }
        //System.out.println(module_fieldList_temp);

        return json2;
    }




}
